package com.ant.business.ocr.controller;

import com.github.pagehelper.PageInfo;
import com.ant.common.utils.FastMap;
import com.ant.common.result.ResponseResult;
import com.ant.common.exceptions.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.ant.business.ocr.entity.Explorer;
import com.ant.business.ocr.service.ExplorerService;

import java.util.List;
import java.util.Objects;

/**
 * 控制层
 *
 * @description
 * @create 2023-08-16 11:20:20
 */
@RestController
@RequestMapping(value = "/explorer")
@Slf4j
@Api(tags = "")
public class ExplorerController {

    private final ExplorerService explorerService;

    public ExplorerController(ExplorerService explorerService) {
        this.explorerService = explorerService;
    }

    /**
     * 分页查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/list", name = "分页查询列表")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestBody FastMap param){
        try {
            PageInfo<Explorer> pages = explorerService.getList(param.getInt("page"), param.getInt("limit"), param);
            log.info("分页查询列表成功");
            return ResponseResult.success("分页查询列表成功", pages.getList(), pages.getTotal());
        } catch (Exception e) {
            log.info("分页查询列表失败：", e);
            return ResponseResult.fail("分页查询列表失败：" + e.getMessage());
        }
    }

    /**
     * 查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/allList", name = "查询列表")
    @ApiOperation("查询列表")
    public ResponseResult allList(@RequestBody FastMap param){
        try {
            List<Explorer> list = explorerService.getAllList(param);
            log.info("查询列表成功");
            return ResponseResult.success("查询列表成功", list);
        } catch (Exception e) {
            log.info("查询列表失败：", e);
            return ResponseResult.fail("查询列表失败：" + e.getMessage());
        }
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return ResponseResult
     */
    @PostMapping(value = "/getById", name = "通过编号查询")
    @ApiOperation("通过编号查询")
    public ResponseResult getById(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到编号");
            }

            Explorer explorer = explorerService.getById(primaryKey);
            log.info("通过编号查询成功");
            return ResponseResult.success("通过编号查询成功", explorer);
        } catch (Exception e) {
            log.info("通过编号查询失败：", e);
            return ResponseResult.fail("通过编号查询失败：" + e.getMessage());
        }
    }

    /**
     * 修改（新增、修改）
     *
     * @param explorer explorer
     * @return ResponseResult
     */
    @PostMapping(value = "/edit", name = "修改")
    @ApiOperation("修改（新增、修改）")
    public ResponseResult edit(@RequestBody Explorer explorer){
        try {
            if (Objects.isNull(explorer.getId()) || explorer.getId()<=0){
                //新增
                explorerService.saveOne(explorer);
                log.info("新增成功");
            }else {
                //编辑
                explorerService.update(explorer);
                log.info("修改成功");
            }
            return ResponseResult.success("编辑成功");
        } catch (Exception e) {
            log.info("编辑失败：", e);
            return ResponseResult.fail("编辑失败：" + e.getMessage());
        }
    }

    /**
     * 根据id删除
     *
     * @param primaryKey id
     * @return ResponseResult
     */
    @PostMapping(value = "/delete", name = "删除")
    @ApiOperation("删除")
    public ResponseResult delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到编号");
            }
            explorerService.delete(primaryKey);
            log.info("删除成功");
            return ResponseResult.success("删除成功");
        } catch (Exception e) {
            log.info("删除失败：", e);
            return ResponseResult.fail("删除失败：" + e.getMessage());
        }
    }
}